﻿//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Data.SqlClient;


namespace ParametizerSample
{
    using SqlCommandFilters;
    class Program
    {
        static void Main(string[] args)
        {
            // Let's build a connection string (this would normally be done already)
            SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
            sb.InitialCatalog = "AdventureWorks2014";
            sb.IntegratedSecurity = true;
            sb.DataSource = @"SQLBOBT\SQL2k16CTP3";
            SqlConnection con = new SqlConnection(sb.ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            // Pick one of the TestStatements and assign it to the CommandText property of the SqlCommand object
            cmd.CommandText = TestStatements.existentialSubQueryStmt;

            // Parameterize supports a reparse parameter
            // by default it is true. It will reparse and format the resultant SQL to ensure we have good code
            // if you feel that the performance suffers you can turn off by calling this instead
            // Parameters.Parameterize(ref cmd, false);
            // Parameterize will parse, parameterize and create the parameter collection and modify the CommandText and Parameters collection
            // appropriately
            SqlCommandFilters.Parameters.Parameterize(ref cmd);
            // If you want to see the output of the call you can enhance the following snippet
            // Also allows you to view in a Profiler trace
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                string r = rdr[0].ToString();
                Console.WriteLine(r);
            }
            // Done with connection make sure we close it
            con.Close();
            // Wait for the user to indicate it
            Console.WriteLine(cmd.CommandText);
            Console.ReadLine();
        }
    }
}
